package com.itheima.mobilesafe69.activity;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.service.AddressService;
import com.itheima.mobilesafe69.service.BlackNumberService;
import com.itheima.mobilesafe69.service.WatchDogService;
import com.itheima.mobilesafe69.utils.ConstantValue;
import com.itheima.mobilesafe69.utils.PrefUtils;
import com.itheima.mobilesafe69.utils.ServiceUtil;
import com.itheima.mobilesafe69.view.SettingClickView;
import com.itheima.mobilesafe69.view.SettingItemView;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class SettingActivity extends Activity {
	private SettingItemView siv_update;
	private SettingItemView siv_address;
	private String[] mStrs;
	private SettingClickView siv_address_toast_style;
	private SettingClickView siv_address_location;
	private SettingItemView siv_black_number;
	private SettingItemView siv_app_lock;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		//初始化更新方法
		initUpdate();
		//是否开启归属地方法
		initAddress();
		//初始化吐司样式
		initAddressToastStyle();
		//初始化归属地显示位置
		initAddressLocation();
		//初始化拦截黑名单
		initBlackNumber();
		//初始化程序锁的方法
		initAppLock();
	}

	private void initAppLock() {
		siv_app_lock = (SettingItemView) findViewById(R.id.siv_app_lock);
		//1,让是否选中此开关相应的状态和服务是否运行相互绑定
		//2,判断服务是否正在运行(服务类的全路径,看门狗服务)
		final boolean isRunning = ServiceUtil.isRunning("com.itheima.mobilesafe69.service.WatchDogService", this);
		//3,根据服务开启的状态,去判断此条目是否勾选上
		siv_app_lock.setCheck(isRunning);
		
		//4,开启关闭服务点击事件
		siv_app_lock.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean check = siv_app_lock.isCheck();
				siv_app_lock.setCheck(!check);
				//5,服务的开启关闭
				if(!check){
					//开启
					startService(new Intent(getApplicationContext(),WatchDogService.class));
				}else{
					//关闭
					stopService(new Intent(getApplicationContext(),WatchDogService.class));
				}
			}
		});
	}

	private void initBlackNumber() {
		siv_black_number = (SettingItemView) findViewById(R.id.siv_black_number);
		//1,让是否选中此开关相应的状态和服务是否运行相互绑定
		//2,判断服务是否正在运行(服务类的全路径)
		final boolean isRunning = ServiceUtil.isRunning("com.itheima.mobilesafe69.service.BlackNumberService", this);
		//3,根据服务开启的状态,去判断此条目是否勾选上
		siv_black_number.setCheck(isRunning);
		//4,开启关闭服务点击事件
		siv_black_number.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean check = siv_black_number.isCheck();
				siv_black_number.setCheck(!check);
				//5,服务的开启关闭
				if(!check){
					//开启
					startService(new Intent(getApplicationContext(),BlackNumberService.class));
				}else{
					//关闭
					stopService(new Intent(getApplicationContext(),BlackNumberService.class));
				}
			}
		});
	}

	private void initAddressLocation() {
		siv_address_location = (SettingClickView) findViewById(R.id.siv_address_location);
		siv_address_location.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//开启一个透明的界面activity,处理吐司跟随手势移动过程
				startActivity(new Intent(getApplicationContext(), ToastLocationActvity.class));
			}
		});
		siv_address_location.setTitle("归属地提示框位置");
		siv_address_location.setDes("设置归属地提示框位置");
	}

	private void initAddressToastStyle() {
		//单选框中5组选择条目文字说明
		mStrs = new String[]{"透明","橙色","蓝色","灰色","绿色"};
		
		//处理设置界面第三个条目对应的布局
		siv_address_toast_style = (SettingClickView) findViewById(R.id.siv_address_toast_style);
		
		int toast_style_index = PrefUtils.getInt(this,ConstantValue.TOAST_STYLE_INDEX, 0);
		
		siv_address_toast_style.setTitle("设置归属地显示风格");
		siv_address_toast_style.setDes(mStrs[toast_style_index]);
		
		siv_address_toast_style.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//弹出对话框,选择吐司样式
				showDialog();
			}
		});
	}

	protected void showDialog() {
		Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("请选择归属地样式");
		builder.setIcon(R.drawable.ic_launcher);
		//弹出选择条目对话框时,需要读取上次存储的索引值,作为默认选中条目
		int toast_style_index = PrefUtils.getInt(this,ConstantValue.TOAST_STYLE_INDEX, 0);
		//(单选框中5组选择条目文字说明,默认选中条目索引值,选中某个条目后调用的方法)
		builder.setSingleChoiceItems(mStrs, toast_style_index, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//选中条目后,需要去存储条目索引值,which就是选中条目的索引值
				PrefUtils.putInt(getApplicationContext(),ConstantValue.TOAST_STYLE_INDEX,which);
				//选中条目后,关闭对话框
				dialog.dismiss();
				//在单选框选中了一个条目后,也需要去修改描述显示内容
				siv_address_toast_style.setDes(mStrs[which]);
			}
		});
		
		builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		builder.show();
	}

	private void initAddress() {
		siv_address = (SettingItemView) findViewById(R.id.siv_address);
		//1,让是否选中此开关相应的状态和服务是否运行相互绑定
		//2,判断服务是否正在运行(服务类的全路径)
		final boolean isRunning = ServiceUtil.isRunning("com.itheima.mobilesafe69.service.AddressService", this);
		//3,根据服务开启的状态,去判断此条目是否勾选上
		siv_address.setCheck(isRunning);
		//4,开启关闭服务点击事件
		siv_address.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean check = siv_address.isCheck();
				siv_address.setCheck(!check);
				//5,服务的开启关闭
				if(!check){
					//开启
					startService(new Intent(getApplicationContext(),AddressService.class));
				}else{
					//关闭
					stopService(new Intent(getApplicationContext(),AddressService.class));
				}
			}
		});
	}

	private void initUpdate() {
		//事件传递规则&响应规则
		siv_update = (SettingItemView) findViewById(R.id.siv_update);
		
		//根据在sp中存储的是否更新的状态,去决定初始化展示效果
		boolean is_Update = PrefUtils.getBoolean(this,ConstantValue.UPDATE_VERSION, false);
		siv_update.setCheck(is_Update);
		
		siv_update.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean check = siv_update.isCheck();
				//1,由选中---->未选中
				//2,由未选中--->选中
				siv_update.setCheck(!check);
				PrefUtils.putBoolean(getApplicationContext(), ConstantValue.UPDATE_VERSION, !check);
			}
		});
	}
}
